package wli.web.util;

import java.io.*;
import java.util.*;

public class MyFileWriter  {

    private File name = null;
    private String fileName = null;
    private ObjectOutputStream objectOutput;
    private BufferedOutputStream bufOutput;


    public void setFileName( String file_name )
    {
	fileName = file_name;
	name = new File (fileName);
	if(name.exists()) {
	    name.delete();
	    System.out.println("Deleted fiel:"+name.getPath());
	}
    }

    public void useObjectOutputStream (String inputString) {
	String temp = inputString;

	try {
	    objectOutput =  new ObjectOutputStream(new FileOutputStream( name));
	    objectOutput.writeObject(temp);
	    objectOutput.close();
	}
	catch( IOException e ) {
	    
	}
    }

    public void useDataOutputStream (String inputString) {
	String temp = inputString;

	try {

	    DataOutputStream dos =  new DataOutputStream(new FileOutputStream( name));
	    // dos.writeChars(temp); //16 bits
	    dos.writeBytes(temp);    // 8 bits
	    dos.close();
	}
	catch( IOException e ) {
	    
	}
    }

    public void useFileOutputStream (String inputString) {
	String temp = inputString;

	try {

	    FileOutputStream fos =  new FileOutputStream(fileName);
	    for (int i=0; i <temp.length(); i++) {
		fos.write(temp.charAt(i));
	    }

	    fos.close();
	}
	catch( IOException e ) {
	    
	}
    }

    public boolean useBufferedOutputStream (String inputString) {
	String temp = inputString;

	try {
	    bufOutput =  new BufferedOutputStream(new FileOutputStream( name));
	    for (int i =0; i <temp.length(); i++) {
		bufOutput.write(temp.charAt(i));
	    }
	    bufOutput.flush();
	    bufOutput.close();
	    return true;
	}
	catch( IOException e ) {
	    System.out.println("MyFileWriter.useBufferOutputStram:"+e);
	    return false;
	}
    }

    public void useBufferedWriter (String inputString) {
	String temp = inputString;

	try {
	    FileWriter fw = new FileWriter(fileName);
	    BufferedWriter bfw =  new BufferedWriter ( fw );
	    bfw.write(temp);
	    bfw.flush();
	    bfw.close();
	}
	catch( IOException e ) {
	    
	}
    }

    public static void main (String args[]) {
	MyFileWriter myWriter = new MyFileWriter();
	StringBuffer myBuffer = new StringBuffer();
	String dir = "./";

	myBuffer.append("AAAAAAAA"+"\n");
	myBuffer.append("BBBBBBBB"+"\n");

	if (args[0].equals("1")){
	    myWriter.setFileName(dir+"myObjectOutputStream.txt");
	    myWriter.useObjectOutputStream(myBuffer.toString());
	}
	if (args[0].equals("2")) {
	    myWriter.setFileName(dir+"myBufferedOutputStream.txt");
	    myWriter.useBufferedOutputStream(myBuffer.toString());
	}

	if (args[0].equals("3")) {
	    myWriter.setFileName(dir+"myDataOutputStream.txt");
	    myWriter.useDataOutputStream(myBuffer.toString());
	}

	if (args[0].equals("4")) {
	    myWriter.setFileName(dir+"myFileOutputStream.txt");
	    myWriter.useFileOutputStream(myBuffer.toString());
	}

	if (args[0].equals("5")) {
	    myWriter.setFileName(dir+"myBufferedWriter.txt");
	    myWriter.useBufferedWriter(myBuffer.toString());
	}
    }
}



